package com.slz3.business.module.wmsManagement.wmsPurchaseOrderLine.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.wmsManagement.wmsPurchaseOrderLine.domain.WmsPurchaseOrderLine;
import com.slz3.business.module.wmsManagement.wmsPurchaseOrderLine.domain.WmsPurchaseOrderLineFactory;
import com.slz3.business.module.wmsManagement.wmsPurchaseOrderLine.steward.WmsPurchaseOrderLineSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class WmsPurchaseOrderLineServiceImpl implements WmsPurchaseOrderLineService {
    @Override
    public WmsPurchaseOrderLine save(WmsPurchaseOrderLine wmsPurchaseOrderLine) {
        return Repositorys.wmsPurchaseOrderLine.save(WmsPurchaseOrderLineFactory.initBaseFields(wmsPurchaseOrderLine));
    }
    @Override
    public Page<WmsPurchaseOrderLine> page(MultiValueMap<String, String> params) {
        Page<WmsPurchaseOrderLine> wmsPurchaseOrderLinePage = Repositorys.wmsPurchaseOrderLine.findAll(
                WmsPurchaseOrderLineSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return wmsPurchaseOrderLinePage;
    }

    @Override
    public List<WmsPurchaseOrderLine> saveList(List<WmsPurchaseOrderLine> wmsPurchaseOrderLines) {
        return Repositorys.wmsPurchaseOrderLine.saveAll(wmsPurchaseOrderLines);
    }

    @Override
    public WmsPurchaseOrderLine modify(WmsPurchaseOrderLine wmsPurchaseOrderLineNew) {
        return Repositorys.wmsPurchaseOrderLine.saveAndFlush(WmsPurchaseOrderLineFactory.initBaseFields(wmsPurchaseOrderLineNew));
    }

    @Override
    public Optional<WmsPurchaseOrderLine> single(UUID uuid) {
        return Repositorys.wmsPurchaseOrderLine.findById(uuid);
    }

    @Override
    public List<WmsPurchaseOrderLine> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsPurchaseOrderLine.findAllById(uuids);
    }

    @Override
    public List<WmsPurchaseOrderLine> delete(List<WmsPurchaseOrderLine> wmsPurchaseOrderLineList) {
        wmsPurchaseOrderLineList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsPurchaseOrderLineFactory.initBaseFields(item);
        });
        return Services.wmsPurchaseOrderLineTransactional.batchModify(wmsPurchaseOrderLineList);
    }

}
